package com.aaa.marketresearch.android.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.data.domain.Market;
import com.aaa.marketresearch.android.service.HttpClient;
import com.aaa.marketresearch.android.util.SimpleLog;
import com.aaa.marketresearch.android.util.connector.IDataTextResponse;


/**
 * @author Nail
 */
public class MarketEditScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_market_edit_screen);
    }

    @Override
    protected void onResume() {
        super.onResume();

        final Market market = UserInfo.getInstance().getCurrentMarket();

        EditText nameEditText =(EditText)findViewById(R.id.marketNameField);
        nameEditText.setText(market.getName());

        EditText descriptionEditText =(EditText)findViewById(R.id.marketDescriptionField);
        descriptionEditText.setText(market.getDescription());
    }


    public void saveButtonClick(View view) {
        SimpleLog.info("saveButtonClick");

        final Market market = UserInfo.getInstance().getCurrentMarket();

        String name = ((EditText) findViewById(R.id.marketNameField)).getText().toString();
        market.setName(name);

        String description = ((EditText) findViewById(R.id.marketDescriptionField)).getText().toString();
        market.setDescription(description);


        HttpClient httpClient = new HttpClient(this.getApplicationContext());
        httpClient.saveMarket(market, new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                SimpleLog.info("saved");
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MarketEditScreen.this);
                alertDialog.setMessage("Сохранен");
                alertDialog.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                MarketEditScreen.this.finish();
                            }
                        }
                );
                alertDialog.show();
            }

            @Override
            public void onError() {
                SimpleLog.info("error");
            }
        });
    }
}